//
//  GetCollectionMediasOperation.m
//  Vmix
//
//  Created by jeffs on 6/16/2010.
//  Copyright 2010 VMIX Media Inc.. All rights reserved.
//

#import "GetCollectionMediasOperation.h"
#import "VmixVideo.h"
#import <arpa/inet.h> // For AF_INET, etc.
#import <ifaddrs.h> // For getifaddrs()
#import <net/if.h> // For IFF_LOOPBACK


@implementation GetCollectionMediasOperation
@synthesize mediaData, mediaArray, delegate, collectionMediasApi;

//Check for WIFI connection.
BOOL localWiFiAvailable()
{
    struct ifaddrs *addresses;
    struct ifaddrs *cursor;
    BOOL wiFiAvailable = NO;
    if (getifaddrs(&addresses) != 0) return NO;
	
    cursor = addresses;
    while (cursor != NULL) {
        if (cursor -> ifa_addr -> sa_family == AF_INET
			&& !(cursor -> ifa_flags & IFF_LOOPBACK)) // Ignore the loopback address
        {
			// Check for WiFi adapter
			if (strcmp(cursor -> ifa_name, "en0") == 0) {
				wiFiAvailable = YES;
				break;
			}
        }
        cursor = cursor -> ifa_next;
    }
	
    freeifaddrs(addresses);
    return wiFiAvailable;
}

- (void)main {
	
	NSURL *url = [NSURL URLWithString: self.collectionMediasApi];
	
	CXMLDocument *apiParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];
    self.mediaArray = [[NSMutableArray alloc] init];
	NSArray *resultNodes = NULL;
    resultNodes = [apiParser nodesForXPath:@"//media" error:nil];
	NSString *formatId = [[[NSString alloc] init] autorelease];
	//Switch formats based on available connection type
	if(localWiFiAvailable()){
		formatId = WIFI_FORMAT;
	}else{
		formatId = CEll_FORMAT;
	}
	
    for (CXMLNode *resultNode in resultNodes) {
		
		VmixVideo *video = [[VmixVideo alloc] init];
		video.media_id = [resultNode getElementValue:@"id" error:nil];
        // media_id is required for a complete video object.  If it doesn't exist
        // skip over this node value.
        if( video.media_id == (NSString *)[NSNull null] || video.media_id.length == 0 ) {
            continue;
        }
		
		video.partner_id = [resultNode getElementValue:@"partner_id" error:nil];
		video.title = [resultNode getElementValue:@"title" error:nil];
		video.description = [resultNode getElementValue:@"description" error:nil];
		video.class_id = [resultNode getElementValue:@"class_id" error:nil];
		video.date_created = [resultNode getElementValue:@"date_created" error:nil];
		video.date_approved = [resultNode getElementValue:@"date_approved" error:nil];
		video.last_modified =[ resultNode getElementValue:@"last_modified" error:nil];
		video.source_name = [resultNode getElementValue:@"source_name" error:nil];
		video.author = [resultNode getElementValue:@"id" error:nil];
		video.url = [resultNode getElementValue:@"url" error:nil];
		NSArray *formatNodes= NULL;
		NSArray *thumbNodes = NULL;
		formatNodes = [resultNode nodesForXPath:@"formats/format" error:nil];
		thumbNodes = [resultNode nodesForXPath:@"thumbnail" error:nil];
		
		for (CXMLNode *formatNode in formatNodes) {
			
			if([[formatNode getElementValue:@"format_id" error:nil] isEqual:formatId ]){
				video.token = [formatNode getElementValue:@"token" error:nil];
			}
		}
		if(video.token == nil){
			video.token = [resultNode getElementValue:@"token" error:nil];
		}
		for (CXMLNode *thumbNode in thumbNodes) {
			
			if([[thumbNode getElementValue:@"is_default" error:nil] isEqual:@"1" ]){
				video.thumbnail = [thumbNode getElementValue:@"url" error:nil];
			}
		}
		
        [self.mediaArray addObject:video];
		//[video release];
		//[formatNodes release];
		//[thumbNodes release];
    }	
	
	[self.delegate didFinishParsingCollectionMedias:self.mediaArray];
	
}


@end
